import { getToken } from '@/utils'
import router, { whitelistRouterMap } from '.'
import { useUserStore } from '@/stores'

router.beforeEach(async (to, from, next) => {
  // 已登陆
  if (getToken()) {
    const userStore = useUserStore()
    await userStore.getUseInfo()

    if (userStore.routes.some(item => item.name === to.name || item.path === to.path)) {
      // 解决刷新页面空白页
      to.name === undefined ? next({ path: to.path }) : next()
      return
    }

    // 白名单路由
    if (whitelistRouterMap.some(item => item.name === to.name || item.path === to.path)) {
      next()
      return
    }
  } else {
    next()
  }

  next('/404')
})
